
import time
import random
import sys
from matrix_game import game_base, Sprite, RIGHT_MEET, LEFT_MEET,UP_MEET,DOWN_MEET
import matrix_game
import cyberpi
matrix_game.FACE_ROW = 16
matrix_game.FACE_CLO = 16
# 俄罗斯方块游戏代码
game = matrix_game.game_base()

score = 0
a_s = '10101010000000000000000000000000'
b_s = '10380000000000000000000000000000'
c_s = '30300000000000000000000000000000'
d_s = '10180800000000000000000000000000'
e_s = '30101000000000000000000000000000'
f_s = '08181000000000000000000000000000'

sprite_num = 6
sprites = [a_s, b_s, c_s, d_s, e_s, f_s]
current_script = None
move_lock_flag = False

move_flag = 0
down_speed = 10

def line_remove_process():
    global score
    screen = game.get_screen()
    background = game.get_background()
    removed_flag = False
    i = 0
    while i < matrix_game.FACE_CLO:
        if screen[i] & 0xffff == 0xffff:
            score += 1
            removed_flag = True
            del background[i]
            background.insert(0, 0x0000)
        i += 1
    if removed_flag:
        cyberpi.audio.play("score.mp3")
    game.set_background(background)

def move_control():
    global down_speed, current_script, move_lock_flag
    global quit
    if current_script == None :
        time.sleep(0.2)
        return

    if cyberpi.controller.is_press('left') and move_lock_flag == False:
        current_script.up()
        if game.background_collision_check(current_script):
            current_script.down()
        while cyberpi.controller.is_press('left'):
            pass 

    elif cyberpi.controller.is_press('right') and move_lock_flag == False:
        current_script.down()
        if game.background_collision_check(current_script):
            current_script.up()
        while cyberpi.controller.is_press('right'):
            pass 

    elif cyberpi.controller.is_press('a') and move_lock_flag == False:
        current_script.rotate()
        while cyberpi.controller.is_press('a'):
            pass 
    elif cyberpi.controller.is_press('down') and move_lock_flag == False:
        down_speed = 2
        while cyberpi.controller.is_press('down'):
            pass 

    temp = current_script.meet_border_check()
    if temp == UP_MEET:
        current_script.down()
        while current_script.meet_border_check() == UP_MEET:
            current_script.down()
    elif temp == DOWN_MEET:
        current_script.up()
        while current_script.meet_border_check() == DOWN_MEET:
            current_script.up()

def work():
    global current_script, down_speed, score, move_lock_flag
    game.game_start()
    game.set_background([0] * 16)
   
    while True:
        index = random.randint(0, sprite_num - 1)
        current_script = Sprite(sprites[index])
        game.add_sprite(current_script)
        current_script.left(2)
        if game.background_collision_check(current_script):
            game.del_sprite(current_script)
            current_script = None
            game.game_over()
            cyberpi.audio.play("wrong.mp3")
            cyberpi.console.println("您的得分：")
            cyberpi.console.println(score)
            score = 0
            break
        count = 1
        down_speed = 10
        game.screen_refresh()
        while True:
            if count % down_speed == 0:
                current_script.right()
            
            if current_script.meet_border_check() == RIGHT_MEET:
                move_lock_flag = True
                current_script.left()
                break

            if game.background_collision_check(current_script):
                move_lock_flag = True
                current_script.left()
                break

            move_control()
            # 屏幕刷新函数
            game.screen_refresh()

            time.sleep(0.01)
            count += 1
        game.screen_refresh()
        time.sleep(0.04)
        game.set_background(game.get_screen())
        current_script.hide()
        time.sleep(0.1)
        line_remove_process()
        game.del_sprite(current_script)
        move_lock_flag = False
        # 屏幕刷新函数
        game.screen_refresh()

while True:
    work()
    
    cyberpi.console.println("\n按 A键重新开始")
    while not cyberpi.controller.is_press('a'):
        pass
